package com.example.acceletracks;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import com.google.android.gms.maps.model.LatLng;

import android.location.Location;
import android.os.Environment;
import android.util.Log;

public class IOManager {
	
	static String mDirName="_MyTrack";
	
	
	public static void writeStringAsFile(String string, String name, String dir) {
		
		File path = Environment.getExternalStorageDirectory();
		File directory = new File(path+File.separator+dir);
		if(!directory.exists())
			directory.mkdir();
		
		File out_plik = new File(name);
		FileWriter fileWriter;
		Log.d("MyIOExeption", "IOManager::path " + path + File.separator + dir + File.separator + out_plik);
		try {
			fileWriter = new FileWriter(path + File.separator + dir + File.separator + out_plik);
			BufferedWriter zapis = new BufferedWriter(fileWriter);
			zapis.write(string);
			zapis.close();
		} catch (IOException e) {
			Log.e("MyIOExeption", "IOExeption: IOManager:writeStringAsFile");
		}
	}
	
	public static String readFileAsString(String plik_in, String dirName) {
		
		StringBuffer buffer =new StringBuffer();
		File path = Environment.getExternalStorageDirectory();
		
		File in_plik = new File(path + File.separator + dirName +File.separator + plik_in);
		FileReader fileReader;
		try {
			fileReader = new FileReader(in_plik);
			BufferedReader odczyt = new BufferedReader(fileReader);
			String line = odczyt.readLine();
			
			while(line!=null)
			{
				buffer.append(line +"\n");
				
				line=odczyt.readLine();
			}
			
			odczyt.close();
		} catch (IOException e) {
			Log.d("MyIOExeption", "IOManager:readFileAsString");
		}

		return buffer.toString();
	}
	
	public static int writeAcceleHolesToDb(String holesString, Handler dbHandler, float radius){
		
		AcceleHoles ah = new AcceleHoles(convertStringToLatLngList(holesString));
		int amount = 0;
		
		for (int i = 0; i < ah.getLength(); i++) {
	
			LatLng latlng = (LatLng) ah.toArray()[i];
			if(!dbHandler.isItem(latlng) && !DistanceManager.isClose(latlng, dbHandler, radius))
			{
				dbHandler.addItem(latlng);
				amount+=1;
			}
		}
		
		return amount;	
	}

	public static List<LatLng> convertStringToLatLngList(String path) {
		List<LatLng> list = new LinkedList<LatLng>();

		if (path != null && path.trim().length() > 0) {

			String[] pairs = path.trim().split("\n");
			String[] lngLat;

			for (int i = 0; i < pairs.length; i++) {
				lngLat = pairs[i].split(",");

				LatLng p = new LatLng(Double.parseDouble(lngLat[0]),
						Double.parseDouble(lngLat[1]));
				list.add(p);
			}

		}
		return list;

	}

	
	public static List<Float> convertStringToSpeedList(String route) {
		
		List<Float> list = new LinkedList<Float>();
		if (route != null && route.trim().length() > 0)
		{
			String[] pairs = route.trim().split("\n");

			for (int i = 0; i < pairs.length; i++)
				list.add(Float.parseFloat(pairs[i].split(",")[2]));			
		}
		
		return list;
	}
	
	public static List<Node> convertStringToNodeList(String acceleRoute)
	{
		List<Node> list = new LinkedList<Node>();
		if (acceleRoute != null && acceleRoute.trim().length() > 0)
		{
			String[] pairs = acceleRoute.trim().split("\n");
			String[] all;

			for (int i = 0; i < pairs.length; i++)
			{
				all=pairs[i].split(";");
				
				float[] values = {Float.parseFloat(all[0]), Float.parseFloat(all[1]), Float.parseFloat(all[2])};
				float time = Float.parseFloat(all[3]);
				float time_diff=Float.parseFloat(all[4]);
				float speed=Float.parseFloat(all[5]);
				LatLng point=new LatLng(Float.parseFloat(all[6]), Float.parseFloat(all[7]));
			
				Node node = new Node(values, time, time_diff, speed, point);
				list.add(node);
			}
		}
		
		return list;
	}

	public static List<Location> convertLatLngToLocation(List<LatLng> list) {

		List<Location> locations = new LinkedList<Location>();
		for (LatLng latlng : list) {			
			Location loc = new Location("");
			loc.setLatitude(latlng.latitude);
			loc.setLongitude(latlng.longitude);
			locations.add(loc);
		}

		return locations;
	}
	
	
}
